package com.yejing.redisdemo;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.*;

import javax.net.ssl.HostnameVerifier;
import java.util.Set;

@Slf4j
public class Demo1 {
    static Jedis jedis = null;

    static{
        if(null == jedis){
            DefaultJedisClientConfig config =
                    DefaultJedisClientConfig.builder() //
                            .password("123456") //
                            .build(); //
            HostAndPort hostPort = new HostAndPort("43.136.67.62", 6379);
            jedis = new Jedis(hostPort, config);
        }
    }

    public static void main(String[] args) {

//      test2();
//      test5();
        test3();
    }

    public static void test1(){

        String ping = jedis.ping(); // 测试连接
        System.out.println(ping); // 如果成功连接上了Redis服务，此处将会输出PONG
        log.info(ping);

    }

    /**
     *
     */
    public static void test2(){

        // 不存在才设置
        long lock = jedis.setnx("lock", "123456");
        jedis.pexpire("lock",1000*10);
        log.info(lock+""); // 1

//        lock = jedis.setnx("lock","23456");
//        log.info(lock+""); // 0

        jedis.close(); // 归还连接

    }

    public static void test3(){
        Set<String> keys = jedis.keys("lock");
        keys.stream().forEach(System.out::println);
        jedis.close(); // 归还连接
    }

    public static void test4(){

        // 设置失效时间30秒，30秒后ttl返回-2
        String setex = jedis.setex("k2", 30, "kkk");
        System.out.println(setex);
        log.info(setex);
        jedis.close();
    }

    public static void test5(){
        long lock = jedis.del("lock");
        log.info(lock+"");
    }
}
